package com.gulimall.seckill.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConf {

//    @Autowired
//    private RabbitTemplate rabbitTemplate;

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("seckill-event-exchange", true, false);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("seckill-order-queue", true, false, false);
    }


    @Bean
    public Binding orderReleaseOrderQueueBinding() {
        return new Binding("seckill-order-queue", Binding.DestinationType.QUEUE, "seckill-event-exchange", "seckill.order.success", null);
    }

//    @PostConstruct
//    public void initRabbitTemplate() {
//
//        // 入队成功回调
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                System.out.println("入队成功correlationData:" + correlationData + "\t ack:" + ack + "\tcause" + cause);
//            }
//        });
//        // 入队异常回调
//        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
//            @Override
//            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
//                System.out.println("入队失败message:" + message + "\t 相应码:" + replyCode + "\t 原因" + replyText + "\t 路由:" + exchange + "\t 路由键：" + routingKey);
//            }
//        });
//
//    }
}
